<?php
$oDataIni = $oDataAge->monthFirst('obj');
$oDataIni = $oDataIni->weekFirst('obj');
$ymdIniz = $oDataIni->getYMD();
$oDataFin = $oDataAge->monthLast('obj');
$oDataFin = $oDataFin->weekLast('obj',$oDataFin,'obj');
$ymdFine = $oDataFin->getYMD();
$numRig = floor($oDataIni->dayDiff($oDataFin, 'obj')/7)+1;
//dbg_($ymdIniz."-".$ymdFine." righe=".$numRig);
$aWhere = array(" a.tipo_agenda <> 'C' ");
$aWhere[] = " a.data_agenda >= '".$ymdIniz."' ";
if ($sListaTI != 'all') {
	$aWhere[] = " (a.tipo_agenda = 'M' OR a.id_tipo_imp IN ($sListaTI)) ";
}

if ($sViewMemo == 'N') {
	$aWhere[] = " a.tipo_agenda = 'I' ";
}

$sWhere = (empty($aWhere))?"":" WHERE ".implode (" AND ",$aWhere);
				
$sSql = "SELECT DISTINCT a.id_agenda, a.giorno, a.mese, a.anno, a.ora, a.minuti ".
		", a.data_agenda, a.data_memo_iniz, a.tipo_agenda \n" .
		" FROM ovf_agenda a \n" .
		" INNER JOIN ovf_agenda_utenti au ON au.id_agenda = a.id_agenda \n".
		"    AND au.id_utente IN ($sListaAgende) \n".
		" $sWhere \n".
		" ORDER BY a.tipo_agenda DESC, a.data_agenda \n";
//dbg_($sSql);
$objDB->query($sSql,'selId');
$aIdY = array();
while($acX = $objDB->fetch('selId')) {
	//dbg_($acX['tipo_agenda'].":".$acX['data_agenda'].":".$ymdFine);
	if ($acX['tipo_agenda'] == 'I' && $acX['data_agenda'] > $ymdFine) break;
	if ($acX['tipo_agenda'] == 'M' && $acX['data_memo_iniz'] > $ymdFine) continue;
	if ($acX['giorno'] == 0) {
		$ymdX = '0000-00-00';
	} elseif ($acX['mese'] == 0) {
		$ymdX = date('Y-m-d',mktime(0,0,0,$oDataIni->month(),$acX['giorno'],$oDataIni->year()));
		if ($ymdX < $oDataIni->getYMD()) {
			$ymdX = date('Y-m-d',mktime(0,0,0,$oDataIni->month()+1,$acX['giorno'],$oDataIni->year()));
		}
	} elseif ($acX['anno'] == 0) {
		$ymdX = date('Y-m-d',mktime(0,0,0,$acX['mese'],$acX['giorno'],$oDataIni->year()));
		if ($ymdX < $oDataIni->getYMD()) {
			$ymdX = date('Y-m-d',mktime(0,0,0,$acX['mese'],$acX['giorno'],$oDataIni->year()+1));
		}
	} else {
		$ymdX = $acX['data_agenda'];
	}
	if ($ymdX != '0000-00-00' && $ymdX > $ymdFine) continue;
	$aIdY[] = "A".$ymdX.lpad($acX['ora'],2,'0').lpad($acX['minuti'],2,'0').$acX['id_agenda'];
}
sort($aIdY);
$oDataX = new ovfDate($oDataIni,'obj');
$meseX = $oDataX->month();
$aIdMemoG = $aId = array();
foreach($aIdY as $kY => $sY) {
	$dataX = substr($sY,1,10);
	if ($dataX == '0000-00-00') { //memo giornaliero
		$aIdMemoG[] = $sY;
		continue;
	}
	$oDataX = new ovfDate($dataX,'ymd');
	$colX = $oDataX->weekDay();
	$rigX = floor($oDataIni->dayDiff($oDataX, 'obj')/7);
	$aId[$rigX][$colX][] = $sY;
}
//dbg_($aId);
$sCmdL  = str_replace(" ","&nbsp;",strtoupper($oDataAge->month('text-long'))." ".$oDataIni->year());

$sCmdR  = "";

$sCmdR  .= "&nbsp;&nbsp;&nbsp";
$sCmdR  .= "<a href='agenView.php?dataAge=".$oDataAge->getMonthAdd(-1)."&$sXSNQS' class='cmd1'>"
	._t("mese prec.")."</a>\n";
$sCmdR  .= "&nbsp;&nbsp;&nbsp";
$sCmdR  .= "<a href='agenView.php?dataAge=".$oDataAge->getMonthAdd(1)."&$sXSNQS' class='cmd1'>"
	._t("mese succ.")."</a>\n";
$sCmdR .=  cmdTorna();
testaHtml('Agenda', 'Mensile',$msgPag, 'agen');
?>
<table border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
   <tr>
      <td class="cmd1" align="left" valign='bottom'>
         <? echo $sCmdL;?>
      </td>
      <td class="cmd1" align="right" valign='bottom'>
         <? echo $sCmdR;?>
      </td>
   </tr>
</table>
<form name='frm1' action='<?=$_SERVER['PHP_SELF']?>' method='post' enctype="multipart/form-data">
<input type='hidden' name='azione' value="...">
<input type='hidden' name='xsn' value="<?=XSN?>">
<table class="tab1" border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
<?php 
echo getHtmlRigaRif(20);
// memo giornalieri
$sCla = 'txt1';
$sDataTxt = "Memo";
foreach($aIdMemoG as $kG => $sG) {
	$sSql = "SELECT a.*, CONCAT(u.nome,' ',u.cognome) AS nominativo FROM ovf_agenda a "
			." LEFT JOIN ovf_agenda_utenti au ON au.id_agenda = a.id_agenda "
			." LEFT JOIN ovf_utenti u ON u.id_utente = au.id_utente "
			." WHERE a.id_agenda = ".substr($sG,15);
	$objDB->query($sSql,'selAge');	
	$acX = $acAge = $objDB->fetch('selAge');
	$sAge = '(';
	while($acX) {
		$sAge .= $acX['nominativo'].",";
		$acX = $objDB->fetch('selAge');
	}
	$sAge = substr($sAge,0,-1).')';
	$sHref = "memoView.php?id=".$acAge['id_agenda']."&$sXSNQS";
	?>
	<tr>
		<td class="lab1" colspan='3' align='center'>
			<?php 
			echo $sDataTxt;
			$sDataTxt = "&nbsp;";
			?>
		</td>
		<td class="txt1" colspan='2' align='left'><b>
		<?php 
			
		echo lpad($acAge['ora'],2,'0').":".lpad($acAge['minuti'],2,'0')."&nbsp;";
		echo "<img src='".OVF_URL_ROOT."/app/_img/ico_memo_g.gif' border='0'/>";
			?></b>
		</td>
		<td class="txt1" colspan='15' align='left'>
			<?//echo $sAge." "?>
			<a href='<?=$sHref?>' class='<?=$sCla?>'>
			<?php 
			echo $acAge['oggetto'];
			?></a>
		</td>
	</tr>
<?php 
}
?>
	<tr>
		<td class='lab1' colspan='3' align='center'><?=_t("Lunedi'")?></td>
		<td class='lab1' colspan='3' align='center'><?=_t("Martedi'")?></td>
		<td class='lab1' colspan='3' align='center'><?=_t("Mercoledi'")?></td>
		<td class='lab1' colspan='3' align='center'><?=_t("Giovedi'")?></td>
		<td class='lab1' colspan='3' align='center'><?=_t("Venerdi'")?></td>
		<td class='lab1' colspan='3' align='center'><?=_t("Sabato")?></td>
		<td class='lab1' colspan='2' align='center'><?=_t("Domenica")?></td>
	</tr>
<?php 
for($rigX=0; $rigX < $numRig; $rigX++) {
	?>
	<tr>
	<?php 
	for($gY=0;$gY<7;$gY++) {
		$oDataX = $oDataIni->getDayAdd($rigX*7+$gY,'obj');
		$sDay = "";
		if ($oDataX->isHoliday()) {
			$sStyle = "color:red;";
		} else {
			$sStyle = "";
		}
		$sDay .= "<b><a href='agenView.php?vistaAge=List&dataAge=".urlencode($oDataX->getDMY())
			."&$sXSNQS' style='$sStyle' class='txt1'>";
		$sDay .= $oDataX->day()."&nbsp;".$oDataX->month('text-long')."</a></b>";
		$colspan = ($gY==7)?2:3;
		if (isset($aId[$rigX][$gY])) {
			foreach($aId[$rigX][$gY] as $kY => $sY) {
				$sSql = "SELECT a.*, CONCAT(u.cognome) AS nominativo "
					.", ti.descr AS TI_des, ti.pallino AS TI_pallino "
					."FROM ovf_agenda a "
					." LEFT JOIN ovf_agenda_utenti au ON au.id_agenda = a.id_agenda "
					." LEFT JOIN ovf_utenti u ON u.id_utente = au.id_utente "
					." LEFT JOIN ovf_agenda_tipi_imp ti ON ti.id_tipo_imp = a.id_tipo_imp \n"
					." WHERE a.id_agenda = ".substr($sY,15);
				$objDB->query($sSql,'selAge');	
				$acX = $acAge = $objDB->fetch('selAge');
				$sAge = '(';
				while($acX) {
				   if (strlen($sAge)>50) {
				      $sAge .= "....";
                  break;
				   } 
				   $sAge .= $acX['nominativo'].",";
					$acX = $objDB->fetch('selAge');
				}
				$sAge = substr($sAge,0,-1).')';
				if ($acAge['tipo_agenda'] == 'M') {
					$sHref = "memoView.php";
				} else {
					$sHref = "impeView.php";
				}
				$sHref .= "?id=".$acAge['id_agenda']."&$sXSNQS";
				if ($acAge['tipo_agenda'] == 'M') {
					$sDay .= "<br/><b>".lpad($acAge['ora'],2,'0').":".lpad($acAge['minuti'],2,'0')."</b>&nbsp;";
					if ($acAge['giorno']==0) {
						$sDay .= "<img src='".OVF_URL_ROOT."/app/_img/ico_memo_g.gif' border='0'/>";
					} elseif ($acAge['mese']==0) {
						$sDay .= "<img src='".OVF_URL_ROOT."/app/_img/ico_memo_m.gif' border='0'/>";
					} elseif ($acAge['anno']==0) {
						$sDay .= "<img src='".OVF_URL_ROOT."/app/_img/ico_memo_a.gif' border='0'/>";
					} else {
						$sDay .= "<img src='".OVF_URL_ROOT."/app/_img/ico_memo_x.gif' border='0'/>";
					}
				} else {
					if ($acAge['durata_minuti'] >= 1440) {
						$sDay .= "<br/><b>giornata</b>";
					} else {
						$sDay .= "<br/><b>".lpad($acAge['ora'],2,'0').":".lpad($acAge['minuti'],2,'0')."&nbsp;-&nbsp;";
						$nT = mktime($acAge['ora'],$acAge['minuti'],0,01,01,2001)+$acAge['durata_minuti']*60;
						$sOraFine = date('H:i',$nT);
						if ($sOraFine > "24:00") $sOraFine = "24:00";
						$sDay .= $sOraFine."</b>";
					}
				}
            if ($acAge['tipo_agenda'] == 'M') {
               $pallinoImg = "trasp.gif" ;
            } elseif ($acAge['TI_pallino']=='') {
               $pallinoImg = "vuoto.png";
            } else {
               $pallinoImg = $acAge['TI_pallino'].".gif" ;
            }
				$sDay .= "<br/><img src=\"".OVF_URL_ROOT."/app/_img/pallino_ti_$pallinoImg\" border='0'/>&nbsp;&nbsp;";
				//$sDay .= "$sAge ";
            $sDay .= "<a href='$sHref' class='txt1'>";
            if (strlen($acAge['oggetto']) > 50) {
					$sDay .= substr($acAge['oggetto'],0,46)." ...";
				} else {
					$sDay .= $acAge['oggetto'];
				}
				$sDay .= "</a>";
			}
		}
		?>
		<td class='txt1' colspan='<?=$colspan?>' align='left' valign='top'><?php echo $sDay;?></td>
		<?php 
	}
	?>
	</tr>
	<?php 
}
	
/*////////////////////////////////////////////////
$conta=0;
$ymdOld = '';
$sCla = 'txt1';
foreach($aIdMemoX as $kX => $aX) {
	if ($kX=='') {
		$sDataTxt = "Memo";
	} else {
		$sDataTxt = $oDataX->getDateText('text-short',$kX,'ymd');
	}
	$sCla = ($sCla=='txt1')?'txt2':'txt1';
	if (count($aX)==0) {
		if ($kX != '') {
	?>
	<tr>
		<td class="<?=$sCla?>" colspan='3' align='right'><b><?php echo $sDataTxt;?></b></td>
		<td class="<?=$sCla?>" colspan='2' align='left'>&nbsp;</td>
		<td class="<?=$sCla?>" colspan='15' align='left'>&nbsp;</td>
	</tr>
	<?php 
		}
		continue;
	}
	foreach($aX as $kY => $sY) {
		$sSql = "SELECT * FROM ovf_agenda WHERE id_agenda = ".intval(substr($sY,15));
		$objDB->query($sSql,'selAge');	
		$acAge = $objDB->fetch('selAge');
		if ($acAge['tipo_agenda'] == 'M') {
			$sHref = "memoView.php";
		} else {
			$sHref = "impeView.php";
		}
		$sHref .= "?id=".$acAge['id_agenda']."&$sXSNQS";
	?>
	<tr>
		<td class="<?=$sCla?>" colspan='3' align='right'><b>
			<?php 
			echo $sDataTxt;
			$sDataTxt = "&nbsp;";
			?></b>
		</td>
		<td class="<?=$sCla?>" colspan='2' align='left'><b>
		<?php 
			
			if ($acAge['tipo_agenda'] == 'M') {
				echo lpad($acAge['ora'],2,'0').":".lpad($acAge['minuti'],2,'0')."&nbsp;";
				if ($acAge['giorno']==0) {
					echo "<img src='".OVF_URL_ROOT."/app/_img/ico_memo_g.gif' border='0'/>";
				} elseif ($acAge['mese']==0) {
					echo "<img src='".OVF_URL_ROOT."/app/_img/ico_memo_m.gif' border='0'/>";
				} elseif ($acAge['anno']==0) {
					echo "<img src='".OVF_URL_ROOT."/app/_img/ico_memo_a.gif' border='0'/>";
				} else {
					echo "<img src='".OVF_URL_ROOT."/app/_img/ico_memo_x.gif' border='0'/>";
				}
			} else {
				if ($acAge['durata_minuti'] >= 1440) {
					echo "giornata";
				} else {
					echo lpad($acAge['ora'],2,'0').":".lpad($acAge['minuti'],2,'0')."&nbsp;-&nbsp;";
					$nT = mktime($acAge['ora'],$acAge['minuti'],0,01,01,2001)+$acAge['durata_minuti']*60;
					$sOraFine = date('H:i',$nT);
					if ($sOraFine > "24:00") $sOraFine = "24:00";
					echo $sOraFine;
				}
			}
			?></b>
		</td>
		<td class="<?=$sCla?>" colspan='15' align='left'>
			<a href='<?=$sHref?>' class='<?=$sCla?>'>
			<?php 
			echo $acAge['oggetto'];
			?></a>
		</td>
	</tr>
<?php 
	}
}
//*/
?>
</table>
</form>
<table border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
   <tr>
      <td class="cmd1" align="left" valign='bottom'>
         <? echo $sCmdL;?>
      </td>
      <td class="cmd1" align="right" valign='bottom'>
         <? echo $sCmdR;?>
      </td>
   </tr>
</table>
<?
codaHtml();
exit();

